package zdy.web1.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;

import zdy.web1.init.mybatisFactory;
import zdy.web1.pojo.Goods;

/**
 * Servlet implementation class goodservlet
 */
@WebServlet("/s/good")
public class goodservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public goodservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	    String method=request.getParameter("method");
	  //  request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");
	    PrintWriter out=response.getWriter();
	    if(method==null) return;
	    String outstr=null;
	    if (method.equals("all")) {
	    	outstr=selectAll();
	    		
	    	}
	    
	    if (outstr==null) return;
	    System.out.println(outstr);
	    out.println(outstr);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	private final String classname="zdy.web1.mapper.GoodsMap.";
	public String selectAll() {
		SqlSession sqlSession = mybatisFactory.getSqlSession();	
		String name = "x";
		List<Goods> lis = sqlSession.selectList(classname+"selectAll"); 
		
		if (lis.isEmpty()) 
		return null;
		else
		return JSON.toJSONString(lis);
	}

}
